package com.slj.service.impl;

import com.slj.entity.Books;
import com.slj.mapper.BooksMapper;
import com.slj.service.BookService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

@Service
@Transactional
public class BookServiceImpl implements BookService {
    private final BooksMapper booksMapper;

    // 推荐使用构造器注入
    public BookServiceImpl(BooksMapper booksMapper) {
        this.booksMapper = booksMapper;
    }

    @Override
    public boolean addBook(Books book) {
        return booksMapper.insert(book) > 0;
    }

    @Override
    public boolean deleteBook(Integer id) {
        return booksMapper.deleteById(id) > 0;
    }

    @Override
    public boolean updateBook(Books book) {
        return booksMapper.updateById(book) > 0;
    }

    @Override
    public Books getBookById(Integer id) {
        return booksMapper.selectById(id);
    }

    @Override
    public List<Books> getAllBooks() {
        return booksMapper.selectAll();
    }

    @Override
    public List<Books> searchBooksByName(String name) {
        return booksMapper.selectByName(name);
    }
}